﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using MyToDo.Common;
using MyToDo.Extensions;
using MyToDo.Service;
using MyToDo.Shared.Dtos;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;

namespace MyToDo.ViewModels.Dialogs
{
    public class LoginViewModel : BindableBase, IDialogAware
    {
        public LoginViewModel(ILoginService loginService, IEventAggregator aggregator,ILogger logger)
        {
            UserDto = new RegisterDto();

            ExcuteCommand = new DelegateCommand<string>(Exceute);
            this.loginService = loginService;
            this.aggregator = aggregator;
            this.logger = logger;
            logger.LogDebug("LoginViewModel init");
        }

        private void Exceute(string obj)
        {
            switch (obj)
            {
                case "Login":
                    Login();
                    break;

                case "Loginout":
                    LoginOut();
                    break;

                case "Go":
                    SelectedIndex = 1;
                    break;
                case "Return":
                    SelectedIndex = 0;
                    break;
                case "Register":
                    Register();
                    break;

                default:
                    break;
            }
        }

        private RegisterDto userDto;

        public RegisterDto UserDto
        {
            get { return userDto; }
            set { userDto = value; }
        }

        private async void Register()
        {
            if (
                string.IsNullOrWhiteSpace(userDto.UserName)
                || string.IsNullOrWhiteSpace(userDto.NewPassword)
                || string.IsNullOrWhiteSpace(userDto.Account)
                || string.IsNullOrWhiteSpace(userDto.Password)
            )
            {
                aggregator.SendMessage("账户名和密码可能为空，请检查账户密码", "Login");
                return;
            }

            if (userDto.Password != userDto.NewPassword)
            {
                aggregator.SendMessage("两次密码不一致，请检查密码", "Login");
                return;
            }
            var registerResult = await loginService.Resgiter(
                new Shared.Dtos.UserDto()
                {
                    Account = UserDto.Account,
                    Password = UserDto.Password,
                    UserName = userDto.UserName,
                }
            );
            if (registerResult != null && registerResult.Status)
            {
                aggregator.SendMessage("注册成功", "Login");
                SelectedIndex = 0;
            }
            else
            {
                aggregator.SendMessage("注册失败", "Login");
            }
        }

        public DelegateCommand<string> ExcuteCommand { get; private set; }

        public string Title { set; get; }

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            LoginOut();
        }

        public void OnDialogOpend(IDialogParameters parameters) { }

        public void OnDialogOpened(IDialogParameters parameters) { }

        private int selectedIndex;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set
            {
                selectedIndex = value;
                RaisePropertyChanged();
            }
        }

        private string account;

        public string Account
        {
            get { return account; }
            set
            {
                account = value;
                RaisePropertyChanged();
            }
        }

        private string password;
        private readonly ILoginService loginService;
        private readonly IEventAggregator aggregator;
        private readonly ILogger logger;

        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                RaisePropertyChanged();
            }
        }

        private async void Login()
        {
            if (string.IsNullOrWhiteSpace(account) || string.IsNullOrWhiteSpace(password))
            {
                return;
            }
            var loginResult = await loginService.Login(
                new Shared.Dtos.UserDto() { Account = account, Password = password }
            );
            if (loginResult != null && loginResult.Status)
            {
                AppSession.UserName = loginResult.Result.UserName;
                RequestClose.Invoke(new DialogResult(ButtonResult.OK));
            }
            else
            {
                aggregator.SendMessage("登录失败，稍后再试", "Login");
            }
        }

        private void LoginOut()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.No));
        }
    }
}
